<?php
namespace app\api\controller;
use think\Controller;
use think\Db;
use app\service\personService;
use app\service\deviceService;
use app\service\visitorService;
use app\service\staffService;
use think\Log;
class Callback extends Controller{
    public function yzl(){
        $path=dir(getcwd())->path.'\callback\\'.date('Ymd');
        if (! file_exists ( $path )) {
            mkdir ( "$path", 0777, true );
        }
        
        $address = $path."\\110811525967.txt";

        parse_str(file_get_contents($address),$userinfo);
        echo "<pre>";
        var_dump($userinfo);
        var_dump(json_decode($userinfo["idCardInfo"],true));
    }

	public function license(){
		$path=dir(getcwd())->path.'\callback\\'.date('Ymd');
        if (! file_exists ( $path )) {
            mkdir ( "$path", 0777, true );
        }
        //$address = $path."\\143022815649.txt";
        $address = $path."\\".date('His').rand(100000,999999).'.txt';
        file_put_contents($address,file_get_contents("php://input"),FILE_APPEND);//将数据读入文本
        parse_str(file_get_contents($address),$userinfo);

        //识别回掉
        if(array_key_exists("photoUrl", $userinfo)){
        	$whe["personGuid"]=$userinfo["personGuid"];
            //var_dump($userinfo);
	        $staffinfo=(new staffService())->staffinfo($whe);
            //var_dump($staffinfo);
            //查询设备名称
            $deviceinfo=(new deviceService())->deviceinfo($userinfo["deviceKey"]);
	        //业主家人租客回掉
	        if(!empty($staffinfo)){
	        	
	        	$data["personGuid"]=$userinfo["personGuid"];
	        	$data["deviceKey"]=$userinfo["deviceKey"];
	        	$data["devicename"]=$deviceinfo["devicename"];

	        	$data["phone"]=$staffinfo["phone"];
	        	$data["name"]=$staffinfo["name"];
	        	$data["person_id"]=$staffinfo["id"];
	        	$data["relation"]=1;
	        	$data["company_id"]=$staffinfo["company_id"];
                $data["company_name"]=$staffinfo["company_name"];

	        	$data["recMode"]=$userinfo["recMode"];
	        	$data["photoUrl"]=$userinfo["photoUrl"];
	        	$data["showTime"]=strtotime($userinfo["showTime"]);
                $t=Db::table("record")
                    ->where("showTime",$data["showTime"])
                    ->where("deviceKey",$data["deviceKey"])
                    ->find();

                if(empty($t)){
                    $id=Db::table("record")->insertGetId($data);
                    $data["id"]=$id;
                    $data["type"]="1";
                    //$this->create_local_img($data);
                }
	        	
                echo 1;
                return ;
	        }

            //访客回调
            $visitorinfo=(new visitorService())->visitorGuid($userinfo["personGuid"]);
            if(!empty($visitorinfo)){
               
                $data["personGuid"]=$userinfo["personGuid"];
                $data["deviceKey"]=$userinfo["deviceKey"];
                $data["devicename"]=$deviceinfo["devicename"];

                $data["phone"]=$visitorinfo["phone"];
                $data["name"]=$visitorinfo["name"];
                $data["person_id"]=$visitorinfo["id"];
                $data["relation"]=2;
                $data["company_id"]=$visitorinfo["company_id"];
                $data["company_name"]=$visitorinfo["company_name"];

                $data["recMode"]=$userinfo["recMode"];
                $data["photoUrl"]=$userinfo["photoUrl"];
                $data["showTime"]=strtotime($userinfo["showTime"]);

                $t=Db::table("record")
                    ->where("personGuid",$data["personGuid"])
                    ->where("showTime",$data["showTime"])
                    ->where("deviceKey",$data["deviceKey"])
                    ->find();
                if(empty($t)){
                    $id=Db::table("record")->insertGetId($data);
                    $data["id"]=$id;
                    $data["type"]="1";
                    //$this->create_local_img($data);
                }
                
                echo 2;
                return ;
            }

            //认证比对
            if(array_key_exists("idCardInfo", $userinfo)){
                $data["personGuid"]=$userinfo["personGuid"];
                $data["deviceKey"]=$userinfo["deviceKey"];
                $data["devicename"]=$deviceinfo["devicename"];

               
                $data["relation"]=2;
               
                $data["recMode"]=$userinfo["recMode"];
                $data["photoUrl"]=$userinfo["photoUrl"];
                $data["idCardInfo"]=$userinfo["idCardInfo"];
                $data["showTime"]=strtotime($userinfo["showTime"]);
                $id=Db::table("record")->insertGetId($data);
                $data["id"]=$id;
                $data["type"]="1";
                echo 3;
                //$this->create_local_img($data);
                // Log::write($userinfo,"回调");
                return ;
            }
           
        }
	}
	public function create_local_img($arr){
		$webset=Db::table("webset")->where("id",1)->find();
		$url=$webset["map_url"]."/index/person/index";
        //$url="http://i24o892560.qicp.vip/parkingHzcity/public/index.php/index/Yzl/index";
        //sleep(2);
        //sleep(20);
		$res=$this->yzl_fsockopen($url,$arr);
	}
	public function test2(){
		$arr["type"]=2;
        $arr["user_type"]=4;
        $arr["user_id"]=10;
		$arr["photoUrl"]="https://ground-images.oss-cn-hangzhou.aliyuncs.com/1BCBD7BC5CD446988478832A5DFFD55A/537C327E0B7A429F89378209A73F75AC/D36E24BE30E744A88742A2C3B83E9971";
		$res=$this->create_local_img($arr);
		return $res;
	}
	public function test(){
		$cashcommand["record_id"]="5";
		$cashcommand["user_type"]="4";
		$cashcommand["photoUrl"]="http://uniubi-device.oss-cn-hangzhou.aliyuncs.com/device/spot/photo/84E0F4213A2901FA/2020-01-09/8A187EC8EC3348AB8DD0ABF6E8C1A944_20200109181610_rgb.jpg";
		
		$url="http://i24o892560.qicp.vip:47478/uface/person.php";
		//$url="http://i24o892560.qicp.vip:47478/parkingHzcity/public/index.php/index/test/test";
		$res=$this->yzl_fsockopen($url,$cashcommand);
		var_dump($res);
	}

	public function yzl_fsockopen($url,$post_data=array(),$cookie=array()){
        $url_arr = parse_url($url);
        $port = isset($url_arr['port'])?$url_arr['port']:80;
        if($url_arr['scheme'] == 'https'){
            $url_arr['host'] = 'ssl://'.$url_arr['host'];
        }
        $fp = fsockopen($url_arr['host'],$port,$errno,$errstr,30);
        if(!$fp) return false;
        $getPath = isset($url_arr['path'])?$url_arr['path']:'/index.php';
        $getPath .= isset($url_arr['query'])?'?'.$url_arr['query']:'';
        $method = 'GET';  //默认get方式
        if(!empty($post_data)) $method = 'POST';
        $header = "$method  $getPath  HTTP/1.1\r\n";
        $header .= "Host: ".$url_arr['host']."\r\n";
        if(!empty($cookie)){  //传递cookie信息
            $_cookie = strval(NULL);
            foreach($cookie AS $k=>$v){
                $_cookie .= $k."=".$v.";";
            }
            $cookie_str = "Cookie:".base64_encode($_cookie)."\r\n";
            $header .= $cookie_str;
        }
        if(!empty($post_data)){  //传递post数据
            $_post = array();
            foreach($post_data AS $_k=>$_v){
                $_post[] = $_k."=".urlencode($_v);
            }
            $_post = implode('&', $_post);
            $post_str = "Content-Type:application/x-www-form-urlencoded; charset=UTF-8\r\n";
            $post_str .= "Content-Length: ".strlen($_post)."\r\n";  //数据长度
            $post_str .= "Connection:Close\r\n\r\n";
            $post_str .= $_post;  //传递post数据
            $header .= $post_str;
        }else{
            $header .= "Connection:Close\r\n\r\n";
        }
        fwrite($fp, $header);
        //echo fread($fp,1024);
        usleep(1000); // 这一句也是关键，如果没有这延时，可能在nginx服务器上就无法执行成功
        fclose($fp);
        return true;
    }
}